package com.paw.hys.sa.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.paw.hys.sa.content.ContentHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author lvzihai
 * @date 2022/8/26
 **/
@Slf4j
@Component
public class SthInterceptor implements HandlerInterceptor {


  @Override
  public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    String val = RandomUtil.randomString(5);
    ContentHolder.setValue(val);
    log.info("SthInterceptor val={}",val);
    ThreadContext.put("traceId", IdUtil.fastSimpleUUID());
    return true;
  }


  @Override
  public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    ContentHolder.remove();
  }
}
